<?php
/**
 * this test case cover Databean-derived classes
 */
class TestDatabeanChild extends UnitTestCase {
	
	function setUp() {
		//$this->bean=new DatabeanChild();
	}
	
	function testAutoValue() {
		$b=new DatabeanChild();
		$this->assertTrue($b->autoValues());
		$this->assertEqual($b->date,date('Y-m-d'));
	}
	
	function testMemberCheck() {
		$b=new DatabeanChild();
		$this->assertTrue($b->memberCheck('int',12));
		$this->assertFalse($b->memberCheck('int','12'));
		$this->assertFalse($b->memberCheck('int',null));
		
		$this->assertFalse($b->memberCheck('choice','choucroute'));
		$this->assertFalse($b->memberCheck('choice',null));
		$this->assertFalse($b->memberCheck('choice',''));
		
		$this->assertFalse($b->memberCheck('date','now'));
		$this->assertFalse($b->memberCheck('date','19760110'));
		$this->assertTrue($b->memberCheck('date','1976-01-10'));
		
		$this->assertFalse($b->memberCheck('custom_setter','some value'));
	}
	
	function testMemberGet() {
		$b=new DatabeanChild();
		$this->assertEqual($b->get('generated'),date('Y-m-d'),'should be generated when getted');
		$this->assertEqual($b->date,date('Y-m-d'),'should have auto value');
		$this->assertEqual($b->get('choice'),'default','should have default value');
		
		$this->assertEqual($b->get('custom_getter'),13,'custom getters should override output filters, %s');
	}
	
	function testMemberGettable() {
		$b=new DatabeanChild();
		$this->assertFalse($b->memberGettable('non_gettable'),'should not be gettable %s');
		$this->assertTrue($b->memberGettable('non_settable'),'should be gettable %s');
		$this->assertTrue($b->memberGettable('int'));
		$this->assertTrue($b->memberGettable('generated'));
		$this->assertFalse($b->memberGettable('new'),'unknown members should not be gettable %s');
		
		$this->assertNull($b->get('non_gettable'));
	}
	
	function testMemberSettable() {
		$b=new DatabeanChild();
		$this->assertFalse($b->memberSettable('non_settable'),'should not be settable %s');
		$this->assertTrue($b->memberSettable('non_gettable'),'should be settable %s');
		$this->assertTrue($b->memberSettable('int'));
		$this->assertFalse($b->memberSettable('generated'),'generated members should not be settable');
		$this->assertFalse($b->memberSettable('new'),'unknown members should not be settable %s');
		
		$this->assertFalse($b->set('non_settable','new value'));
		$this->assertEqual($b->non_settable,'non settable');
	}
	
	function testInputFilter() {
		$b=new DatabeanChild();
		$str='to:toto@test.net hello world';
		$this->assertTrue($b->set('str',$str));
		$this->assertEqual($b->str,strtoupper($str));
		$this->assertTrue($b->set('plus_one',12));
		$this->assertEqual($b->plus_one,13);
	}
	
	function testOutputFilter() {
		$b=new DatabeanChild();
		$b->hello='darling';
		if (!$this->assertEqual($b->get('hello'),'hello darling')) {
			$this->dump($b);
		}
	}

	function testSet() {
		$b=new DatabeanChild();
		$this->assertTrue($b->set('int',12));
		$this->assertFalse($b->set('int','36'));
		$this->assertEqual($b->get('int'),12);
		
		$this->assertFalse($b->set('choice','choucroute'));
		$this->assertFalse($b->set('choice',null));
		$this->assertFalse($b->set('choice',''));
		$this->assertEqual($b->choice,'default');
		
		// custom setter
		$this->assertTrue($b->set('custom_setter','some value'));
		$this->assertEqual($b->custom_setter,'some value:10');
	}
	
	function testListMembers() {
		$b=new DatabeanChild();
		$list=$b->listMembers();
		$this->assertIsA($list,'array');
		$this->assertEqual($list,array(
			'int','bool','str','choice','date'
			,'custom_getter','custom_setter','non_gettable','non_settable'
			,'plus_one','hello'));
		// $this->dump($list);
	}
	
	function testListRequiredMembers() {
		$b=new DatabeanChild();
		$list=$b->listRequiredMembers();
		$this->assertIsA($list,'array');
		$this->assertEqual($list,array(
			'int','bool','str','choice','date'));
	}
	
	function testGlobalChecks() {
		$b=new DatabeanChild();
		$this->assertFalse($b->globalCheck());
		
		$b->set('int',12);
		$b->set('str','string');
		$b->set('bool',true);
		$b->set('choice','pizza');
		$this->assertTrue($b->globalCheck());
		
		// $this->dump($b);
	}
	
	function isPHP5() {
		return substr(phpversion(),0,1)=='5';
	}
}

/**
 * this is the example class tested
 */
class DatabeanChild extends Databean {
	
	function DatabeanChild($data=array()) {
		return $this->__construct($data);
	}
	
	function __construct($data=array()) {
		$this->membersLock(true);
		parent::__construct($data);
	}
	
	/**
	 * @var int a simple integer
	 */
	var $int;
	function _int_check($x) {
		return is_int($x);
	}
	function _int_required() { return true; }
	
	/**
	 * @var bool
	 */
	var $bool=false;
	function _bool_inputfilter($x) {
		return $x?true:false;
	}	
	function _bool_required() { return true; }
	
	/**
	 * @var string will be converted upper case on input
	 */
	var $str;
	function _str_check($x) {
		return is_string($x);
	}
	function _str_inputFilter($x) {
		return strtoupper($x);
	}
	function _str_required() { return true; }
	
	/**
	 * @var string should belong to an items list
	 */
	var $choice='default';
	function _choice_check($x) {
		return in_array($x,array('pizza','hamburger'));
	}
	function _choice_required() { return true; }
	
	/**
	 * @var string a date, MySQL format, settign auto to current
	 */
	var $date;
	function _date_autoValue() {
		return date('Y-m-d');
	}
	function _date_check($x) {
		return ereg('[0-9]{4}-[0-9]{2}-[0-9]{2}',$x);
	}
	function _date_required() { return true; }
	
	/**
	 * @var string an undeclared, generated member
	 */
	function _generated_get() {
		return date('Y-m-d');
	}
	
	/**
	 * @var string a member with a custom getter, overriding output filter
	 */
	var $custom_getter='custom getter';
	function _custom_getter_get() {
		return strlen($this->custom_getter);
	}
	function _custom_getter_outputFilter($x) {
		return strtoupper($x);
	}
	
	/**
	 * @var string a member with its custom setter, overriding checks and input filter
	 */
	var $custom_setter='custom setter';
	function _custom_setter_set($x) {
		// echo '<p>used custom setter </p>';
		$this->custom_setter=$x.':'.strlen($x);
		return true;
	}
	function _custom_setter_inputFilter($x) {
		return strtoupper($x);
	}
	function _custom_setter_check($x) {
		// only numeric strings
		return ereg('^[0-9]+$',$x);
	}
	
	/**
	 * @var string a "non-gettable" member
	 */
	var $non_gettable='non gettable';
	function _non_gettable_gettable() {
		return false;
	}
	
	/**
	 * @var string an "non-settable" member
	 */
	var $non_settable='non settable';
	function _non_settable_settable() {
		return false;
	}
	
	/**
	 * @var int will be set as value+1
	 */
	var $plus_one;
	function _plus_one_inputFilter($x) {
		return intval($x)+1;
	}
	function _plus_one_check($x) {
		return is_int($x);
	}
	
	/**
	 * @var string will be outputted as 'hello '.value
	 */
	var $hello;
	function _hello_outputFilter($x) {
		return 'hello '.$x;
	}
}

?>